//
//  HLSystemRingTool.m
//  YueYue
//
//  Created by hulu001 on 2023/6/20.
//

#import "HLSystemRingTool.h"

@interface HLSystemRingTool () <AVAudioPlayerDelegate>
{
    AVAudioSession *_audioSession;
    AVAudioPlayer *_player;
}

@end

@implementation HLSystemRingTool

- (instancetype)init
{
    if (self = [super init]) {
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        
        _audioSession = [AVAudioSession sharedInstance];
        [_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
        [_audioSession setActive:YES error:nil];
    }
    return self;
}

+ (instancetype)shareInstance
{
    static HLSystemRingTool *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)playVideoCalling
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Ring.bundle/rtc_ring" ofType:@"mp3"]];
    if (!url) {
        return;
    }
    
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    
    _player.numberOfLoops = 1;
    [_player prepareToPlay];
    [_player play];
}

- (void)playAudioCalling
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Ring.bundle/rtc_ring" ofType:@"mp3"]];
    if (!url) {
        return;
    }
    
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    
    _player.numberOfLoops = 1;
    [_player prepareToPlay];
    [_player play];
}

- (void)playMessageing
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"message" ofType:@"mp3"]];
    if (!url) {
        return;
    }
    
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    [_player prepareToPlay];
    [_player play];
}

- (void)finishPlay
{
    if (_player && _player.isPlaying) {
        [_player stop];
    }
}

- (void)playRevievedMessage
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Ring.bundle/rtc_ring" ofType:@"mp3"]];
    if (!url) {
        return;
    }
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    _player.volume = 1;
    [_player prepareToPlay];
    [_player play];
}

- (void)playLoveBell
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Ring.bundle/audio_love_bell" ofType:@"wav"]];
    if (!url) {
        return;
    }
    
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    
    [_player prepareToPlay];
    [_player play];
}

- (void)playMoney
{
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Ring.bundle/rtc_ring" ofType:@"mp3"]];
    if (!url) {
        return;
    }
    
    [self configAudiosession];
    
    NSError *error = nil;
    
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        return;
    }
    
    [_player prepareToPlay];
    [_player play];
}

- (void)configAudiosession
{
    [_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
    [_audioSession setActive:YES error:nil];
}

#pragma mark -

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
{
    [_player stop];
}

@end
